home *** CD-ROM | disk | FTP | other *** search
/ Zoom 2 / Zoom - Release 2 (1996)(Active Software)[!].iso / programming / amiga / muibuilder / mb / developer / c / sources_gencodec / writeexternalfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-29  |  2.9 KB  |  122 lines

  1. #include "WriteExternalFile.h"
  2. #include "Tools.h"
  3. #include "MB_protos.h"
  4. #include "MB_pragmas.h"
  5. #include "mb.h"
  6.  
  7. #include <exec/types.h>
  8. #include <clib/dos_protos.h>
  9. #include <clib/exec_protos.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. extern void Quit(void);
  15.  
  16. /****************************************************************************************************************/
  17. /*****                                                                                                        *****/
  18. /**                                         WriteExternalFile                                                       **/
  19. /*****                                                                                                        *****/
  20. /****************************************************************************************************************/
  21.  
  22. /* Create a file where are the external variables and functions declarations */
  23. BOOL WriteExternalFile(char *Externals,ULONG varnb)
  24. {
  25.     int                                i;
  26.     ULONG                            length, type;
  27.     BPTR                            TMPfile;
  28.     char                            *adr_file = NULL;
  29.     __aligned struct FileInfoBlock  Info;
  30.     BOOL                            bool_aux = FALSE;
  31.     char                            *varname;
  32.     char                            *tmp;
  33.     BOOL                            result = FALSE;
  34.     FILE                            *file;
  35.     BOOL                            ExternalExist=FALSE;
  36.  
  37.     /* If the file already exists, we load it in memory */
  38.     if (TMPfile = Open(Externals, MODE_OLDFILE))
  39.     {
  40.         ExamineFH(TMPfile, &Info);
  41.         length = Info.fib_Size;
  42.         if (!(adr_file = AllocMemory(length+1)))
  43.         {
  44.             Close(TMPfile);
  45.             Quit();
  46.         }
  47.         Read( TMPfile, adr_file, length);
  48.         adr_file[length] = '\0';
  49.         Close(TMPfile);
  50.     }
  51.  
  52.     for(i=0;!ExternalExist && i<varnb;i++)
  53.     {
  54.         MB_GetVarInfo(i,MUIB_VarType,&type,TAG_END);
  55.         ExternalExist=(type==TYPEVAR_EXTERNAL || type==TYPEVAR_HOOK);
  56.     }
  57.  
  58.     if (ExternalExist && (file = fopen(Externals, "a+")))
  59.     {
  60.         if (!TMPfile)
  61.             fprintf(file,"#include <exec/types.h>\n\n");
  62.  
  63.         for(i=0;i<varnb;i++)
  64.         {
  65.             MB_GetVarInfo (i,
  66.                               MUIB_VarType, &type,
  67.                               MUIB_VarName, &varname,
  68.                            TAG_END
  69.                            );
  70.             switch(type)    /* if the declaration doesn't exist, we generate it */
  71.             {
  72.                 case TYPEVAR_EXTERNAL:
  73.                     if (!(tmp=AllocMemory(strlen(varname)+14)))
  74.                     {
  75.                         fclose(file);
  76.                         Quit();
  77.                     }
  78.                     strcpy(tmp,"extern int ");
  79.                     strcat(tmp,varname);
  80.                     strcat(tmp,";\n");
  81.                     if (adr_file) 
  82.                         bool_aux = (strstr(adr_file,tmp)!=NULL);
  83.                     if (!bool_aux) 
  84.                         fprintf(file,tmp);
  85.                     FreeMemory(tmp);
  86.                     break;
  87.  
  88.                 case TYPEVAR_HOOK:
  89.                     if (!(tmp=AllocMemory(strlen(varname)+52)))
  90.                     {
  91.                         fclose(file);
  92.                         Quit();
  93.                     }
  94.                     strcpy(tmp,"extern APTR ");
  95.                     strcat(tmp,varname);
  96.                     strcat(tmp,"( struct Hook *a0, APTR a2, APTR a1 );\n");
  97.                     if (adr_file) 
  98.                         bool_aux = (strstr(adr_file,tmp)!=NULL);
  99.                     if (!bool_aux) 
  100.                         fprintf(file,tmp);
  101.                     FreeMemory(tmp);
  102.                     break;
  103.             }
  104.         }
  105.         fclose(file);
  106.     }
  107.     if (adr_file) 
  108.         FreeMemory(adr_file);
  109.  
  110.     if (TMPfile = Open(Externals, MODE_OLDFILE))    /* if the file is 0 bytes long : we remove it */
  111.     {
  112.         ExamineFH(TMPfile, &Info);
  113.         Close(TMPfile);
  114.         length = Info.fib_Size;
  115.         if (length == 0) 
  116.             DeleteFile(Externals);
  117.         else         
  118.             result = TRUE;
  119.     }
  120.     return(result);
  121. }
  122.